package lib.random;

import lib.util.StringUtil;

/**
 * Generates random matrices
 * 
 * @author dolphinigle
 *
 */
public class RandomMatrix {

  /**
   * Random source
   */
  protected ExtendedRandom random_;

  /**
   * Constructs a random graph generator
   * @param random base random seed source
   */
  public RandomMatrix(ExtendedRandom random) {
    random_ = random;
  }
  
  /**
   * Constructs a random matrix of size n*m with alphabet drawn from input.
   */
  public char[][] RandomMatrixFromAlphabet(
      int rows, int columns, String alphabets) {
    String[] ret = new String[rows];
    
    for (int i = 0; i < rows; ++i) {
      ret[i] = random_.nextString(columns, alphabets, /* distinct = */ false);
    }
    
    return StringUtil.StringArrayToCharArray(ret);
  }
  
  /**
   * Randomly overwrites a portion of a matrix. Does not modify input.
   */
  public char[][] RandomOverwrite(
      char[][] matrix, int overwrites, String overwritter) {
    assert (overwrites <= matrix.length * (matrix[0]).length);
    char[][] matrix_copy = new char[matrix.length][matrix[0].length];
    for (int i = 0; i < matrix.length; ++i)
      for (int j = 0; j < matrix[0].length; ++j)
        matrix_copy[i][j] = matrix[i][j];
    
    int[] places = random_.nextCombination(
        matrix.length * matrix[0].length, overwrites);
    for (int i : places) {
      matrix_copy[i / matrix[0].length][i % matrix[0].length] =
          random_.nextChar(overwritter);
    }
    return matrix_copy;
  }

}
